package com.stone.business.jc.action;

/**
 * <pre>
 * 变电运行工区月份工作计划
 * @author 
 * </pre>
 */

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import jxl.read.biff.BiffException;

import com.stone.business.jc.pojo.TJxcp;
import com.stone.business.jc.pojo.TPlanMonth;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TBzgsk;
import com.stone.sys.pojo.TGzltjPara;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.sys.service.UserService;
import com.stone.utils.DateFormatTool;
import com.stone.utils.JsonUtils;
import com.stone.utils.NumUtils;
import com.stone.utils.Resources;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;
import frm.ssh.exception.AppException;
import frm.ssh.util.SpringUtil;

public class PlanMonthAction extends BaseAction {
	
	private static final long serialVersionUID = -4647594073681054487L;

	private JcCommonService myService;

	private String id;
	
	private String ym;
	private String day;
	private String username;
	private String bz;
	
	private List<TPlanMonth> planMonthList;
	// 日均工作量
	 private List<String[]> exp3 = new ArrayList();
	private String isAdmin;
	String conditions = "";
	String conditions1 = "";
	/**
	 * 进入详细页面
	 * @return
	 * @throws AppException
	 */
	public String initDetail() throws AppException {
		
		getRole();

		// 修改进入
		if (!StringUtils.isNullOrBlank(ym)) {
			planMonthList = myService.findListByHql(" from TPlanMonth where to_char(plandate,'YYYY-MM') = '" + ym + "' order by bz ");

			// 新建进入
		} else {

			planMonthList = new ArrayList<TPlanMonth>();

		}

		return SUCCESS;
	}
		
	/**
	 * 保存
	 * @return
	 * @throws AppException
	 */
	public String save() throws AppException {
		
		myService.savePlanMonth(this.planMonthList);
		super.setInfo("保存成功");
		getRole();
		return SUCCESS;
	}
	
	/**
	 * 删除
	 * @return
	 * @throws AppException
	 */
	public String delete() throws AppException {
		
		myService.deletePlanMonth(id);
		
		return "DEL-SUCCESS";
		
	}
	
	public void deletePlanDetail() throws Exception {
		myService.doDelAll(id, "TPlanMonth");
        super.getResponse().getWriter().print("{msg:'删除成功'}");
	}
	
	private void getRole() {
		
		TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
		
		// 安全员
		if ("006".equals(user.getJxkhgw())) {
			
			this.bz = "安全员";
			
			// 技术员
		} else if ("004".equals(user.getJxkhgw())) {
			
			this.bz = "技术员";
			
			// 操作队队长
		} else if ("005".equals(user.getJxkhgw())) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 维修班长
		} else if ("007".equals(user.getJxkhgw())) {
			
			this.bz = "维修班长";
			
		} else {
			this.bz = "";
		}
		
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
	}
	/**
	 * 日均工作量统计
	 * 
	 * @return
	 * @throws AppException
	 * @throws IOException 
	 * @throws BiffException 
	 */
	@SuppressWarnings("unchecked")
	public String tongji() throws AppException ,BiffException,IOException{

		// 取得工作量统计参数
		List<TGzltjPara> list = myService.findListByHql("from TGzltjPara");

		TGzltjPara para = list.get(0);

		/*-
		 * 建一个二维数组 
		 * 行为人 列为项目  
		 * 列为要算分的项目数量
		 * 处理完后重新组合数组 把第0列为空的（没有用户的）都过滤掉。
		 */
		List<HashMap<String, Object>> tempList = myService.findListBySql("select max(id) as id from sys_user");

		// 以最大的id号作为行
		int row = Integer.parseInt(String.valueOf(tempList.get(0).get("id"))) + 1;
		
		// 开始结束时间
		int yjks = Integer.parseInt(para.getYjks().replace(":", ""));
		int yjjs = Integer.parseInt(para.getYjjs().replace(":", ""));
		/**
		 * 人员
		 */
		StringBuffer s = new StringBuffer();
	    s.append("select u.id as id,");
		s.append("    u.name as name,");
		s.append("    u.ygbh as ygbh,");
		s.append("    TO_CHAR(u.cssj,'yyyy-MM-dd') as cssj,");
		s.append("    (select dic5.item_Name  from sys_user u1,sys_dictionary dic5 where u1.id > 0 and u.id=u1.id and u1.JXKHGWJJXS = dic5.item_Id and  dic5.item_Type = 'JXKH_GWJJXS') as jxkhgwjjxs,");//by wangtao add20120425
		s.append("    (select dic6.item_Name  from sys_user u1,sys_dictionary dic6 where u1.id > 0 and u.id=u1.id and u1.JXKHJJZSXS = dic6.item_Id and  dic6.item_Type = 'JXKH_JJZSXS') as jxkhjjzsxs,");//by wangtao add20120425
		s.append("    u.jxkhflag as jxkhflag,");
		s.append("    u.zzmmflag as zzmmflag,");
		s.append("    dic1.item_Name as bzname,");
		s.append("    dic2.item_Name as gwname,");
		s.append("    dic3.item_Name as gjname,");
		s.append("    (select dic4.item_Name  from sys_user u1,sys_dictionary dic4 where u1.id > 0 and u.id=u1.id and u1.jxkhjz = dic4.item_Id and  dic4.item_Type = 'JXKH_JZ') as jzname");//by wangtao add20110415
		s.append("  from sys_user u,");
		s.append("    sys_dictionary dic1,");
		s.append("    sys_dictionary dic2,");
		s.append("    sys_dictionary dic3");
		
		s.append(" where u.id > 0");
		s.append("   and u.state = '1'");
		s.append("   and u.jxkhflag in ('1', '2','3')");
		s.append("   and u.jxkhbz = dic1.item_Id");
		s.append("   and dic1.item_Type = 'JXKH_BZ'");
		s.append("   and u.jxkhgw = dic2.item_Id");
		s.append("   and dic2.item_Type = 'JXKH_GW'");
		s.append("   and u.jxkhgj = dic3.item_Id");
		s.append("   and dic3.item_Type = 'JXKH_GJ'");
		// 班组
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon5())) {
			s.append("   and u.ORGAN_ID ="+super.getSearchBean().getCon5());
			
		}
		s.append(" order by u.sortid");

		List<HashMap<String, Object>> tempListUser = myService.findListBySql(s.toString());
		
		int i = 0;

		// 为了排序生成的数组 
		int sort[]  = new int[row];
		Arrays.fill(sort, -1);
		
		// 定义统计量数组
		// 计算分数后的
		String str[][] = new String[tempListUser.size()][IConstant.GZLGJ_COL_COUNT];
		String str2[][] = new String[tempListUser.size()][33];//日均工作量
		
		// 先摆上人  sort 索引为用户ID，值为数组序号 从1开始 因为ID最小为1
		for (HashMap<String, Object> map : tempListUser) {
			sort[Integer.parseInt(map.get("id").toString())] = i;
			// 转化一把
			str[i][0] = map.get("name").toString();
			str[i][21] = map.get("jxkhflag").toString();
			str2[i][0] = map.get("name").toString();
			str2[i][32] = map.get("zzmmflag").toString();
			i++;
		}
		
		
		//日均工作量统计
		for( int k=1;k<=31;k++){
			/**
			 * 工单工作日期
			 */
			String varday="";
			if(k<10){
				varday="0"+k;
			}else{
				varday=""+k;
			}
			
			conditions = "(state=6 or state=9) and TO_CHAR(rq,'yyyy-MM-dd') = '" 
				+ ym+"-"+varday+"'";//有票无票工单只统计归档后的。
				
			
			tempList = myService.getListByDHql("czkssj as czkssj, czjssj as czjssj,ycclqk as gzfw, gs as gs, zbz as zbz,zbzname as zbzname,zby as zby,zbyname as zbyname,rq as rq,state as state,bgzr as bzid,planid as planid,gcjs as gcjs", conditions, null, "", "TGzrwd s");
	       String bzidold="";
	       String planidold="";
			for (HashMap<String, Object> map : tempList) {
				String bzid=(String)(map.get("bzid") == null ? "0":  map.get("bzid"));
				String planid=(Long)(map.get("planid") == null ? 0L:  map.get("planid"))+"";
				String state=(String)(map.get("state") == null ? "0":  map.get("state"));//0:9代表有票,5:6代表无票
				// 操作开始日期
				Date czkssjdr = DateFormatTool.formatToDate(DateFormatTool.format(((Date) map.get("czkssj")), false), false);
				// 实际日期
				Date rqdr =DateFormatTool.formatToDate(DateFormatTool.format(((Date) map.get("rq")), false), false);
				long days=1;
				if(state.equals("0")||state.equals("9")){
					days=1;
				}else{
					days= DateFormatTool.dayOfDiff(rqdr, czkssjdr)+1;
				}
				// 工时
				int gs = (Integer) (map.get("gs") == null ? 0 :  map.get("gs"));
				String gzfw=(String)map.get("gzfw");
				// 操作日期判断是否算夜间
				String czkssj = DateFormatTool.formate((Date) map.get("czkssj"), "HHmm");
				String czjssj = DateFormatTool.formate((Date) map.get("czjssj"), "HHmm");
				
				// 总负责人
				String[] zbz = ((String) (map.get("zbz") == null? "" : map.get("zbz"))).split(",");
				// 总负责人姓名
				String zbznames = ((String) (map.get("zbzname") == null? "" : map.get("zbzname")));
				// 成员
				String[] zby = ((String) (map.get("zby") == null? "" : map.get("zby"))).split(",");
				// 成员姓名
				String[] zbynames = ((String) (map.get("zbyname") == null? "" : map.get("zbyname"))).split(",");
				// 如果时间为空 就当 早上 9：30处理 按白天算 。一般不会出现 因为有操作人就有操作时间
				czkssj = "".equals(czkssj) ? "930" : czkssj;
				czjssj = "".equals(czjssj) ? "930" : czjssj;
				// 周末是否
				Date rq = ((Date) (map.get("rq") == null? "" : map.get("rq")));  
		        int n =DateFormatTool.dayOfWeek(rq, false);
				double zmxs=1;
				if (n==6||n==0) {
					zmxs=para.getXk21();
				}
				// 夜间倍数 乘 人/工作 除 实际多少人干了这个工作 
				double yjxs=1;
				if (Integer.parseInt(czkssj) >= yjks || Integer.parseInt(czkssj) <= yjjs || Integer.parseInt(czjssj) >= yjks || Integer.parseInt(czjssj) <= yjjs) {
				yjxs=para.getCz01();
				}
				//工程建设的工作任务单 工时*1.2
				String gcjs=(String)(map.get("gcjs") == null ? "0":  map.get("gcjs"));//1:是工程建设，0,2否
				double gcjsxs=1;
				if(gcjs.equals("1")){
					gcjsxs=1.2;
				}
				// 工作负责人
				for (i = 0; i < zbz.length; i++) {
					
					if ("".equals(zbz[i])) {
						break;
					}
					
					// 20110329
					if (sort[Integer.parseInt(zbz[i])] == -1) {
						continue;
					}
					
				    double fzrxs=1;
				 //   if(!planid.equals(planidold)&&!bzid.equals(bzidold)){//同一计划且同一班组 下只取第一个工作任务单负责人办票
					if(StringUtils.isNullOrBlankFull(gzfw)){
					fzrxs=para.getCz71();
					// 不管几个人都按照一次 
					//str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zbz[i])]][k], para.getCz00(), 1, yjxs*para.getCz12(), 1);
					
				      }else{//一种票
				    	  fzrxs=para.getCz61();
				    	// 不管几个人都按照一次 
						//	str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zbz[i])]][k], para.getCz00(), 1, yjxs*para.getCz02(), 1);
							
				      }
					// planidold=planid;
					// bzidold=bzid;
				   // }
					// 实际工时
					//str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltjCzcs(str2[sort[Integer.parseInt(zbz[i])]][k],gs,1, 1, 1, 1);
					// 实际工时得分
					str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltjCzcs(str2[sort[Integer.parseInt(zbz[i])]][k],gs,1, days*zmxs*fzrxs*yjxs*gcjsxs, 1, 1);
					}
				
				// 成员
				for (i = 0; i < zby.length; i++) {
					
					if ("".equals(zby[i])) {
						break;
					}
					
					if (sort[Integer.parseInt(zby[i])] == -1) {
						continue;
					}
					// 如果成员包括负责人，只 算负责人
					if (zbznames.indexOf(zbynames[i]) != -1) {
						continue;
					}
					// 实际工时
					//str2[sort[Integer.parseInt(zby[i])]][1] = NumUtils.gzltjCzcs(str2[sort[Integer.parseInt(zby[i])]][1],gs, 1, 1, 1, 1);
					// 实际工时得分
					str2[sort[Integer.parseInt(zby[i])]][k] = NumUtils.gzltjCzcs(str2[sort[Integer.parseInt(zby[i])]][k],gs, 1, days*zmxs*yjxs*gcjsxs, 1, 1);
					if(StringUtils.isNullOrBlankFull(gzfw)){
					// 不管几个人都按照一次 
					//str2[sort[Integer.parseInt(zby[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zby[i])]][k], para.getCz00(), 1, yjxs*para.getCz12(), 1);
					}else{
						// 不管几个人都按照一次 
						//str2[sort[Integer.parseInt(zby[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zby[i])]][k], para.getCz00(), 1, yjxs*para.getCz02(), 1);
						
					}
					}
			
			}
			/**
			 * 消缺时间
			 */
			conditions = "TO_CHAR(xcjssj,'yyyy-MM-dd') = '" 
				+ ym+"-"+varday+"'";
			
			tempList = myService.getListByDHql("xcsj as czkssj, xcjssj as czjssj,xcr as zbz,xqcy as zby", conditions, null, "", "TSbqxjl s");

			for (HashMap<String, Object> map : tempList) {
				

				// 操作日期判断是否算夜间
				String czkssj = DateFormatTool.formate((Date) map.get("czkssj"), "HHmm");
				String czjssj = DateFormatTool.formate((Date) map.get("czjssj"), "HHmm");
				
				// 总负责人
				String[] zbz = ((String) (map.get("zbz") == null? "" : map.get("zbz"))).split(",");
				
				// 成员
				String[] zby = ((String) (map.get("zby") == null? "" : map.get("zby"))).split(",");
				// 如果时间为空 就当 早上 9：30处理 按白天算 。一般不会出现 因为有操作人就有操作时间
				czkssj = "".equals(czkssj) ? "930" : czkssj;
				czjssj = "".equals(czjssj) ? "930" : czjssj;
				
				// 夜间倍数 乘 人/工作 除 实际多少人干了这个工作 
				double yjxs=1;
				if (Integer.parseInt(czkssj) >= yjks || Integer.parseInt(czkssj) <= yjjs || Integer.parseInt(czjssj) >= yjks || Integer.parseInt(czjssj) <= yjjs) {
				yjxs=para.getQx01();
				}
				
				// 工作负责人
				for (i = 0; i < zbz.length; i++) {
					
					if ("".equals(zbz[i])) {
						break;
					}
					
					// 20110329
					if (sort[Integer.parseInt(zbz[i])] == -1) {
						continue;
					}
					
					// 不管几个人都按照一次 
					str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zbz[i])]][k], para.getQx00(), 1, yjxs*para.getQx02(), 1);
					
					}
				
				// 成员
				for (i = 0; i < zby.length; i++) {
					
					if ("".equals(zby[i])) {
						break;
					}
					
					if (sort[Integer.parseInt(zby[i])] == -1) {
						continue;
					}
					str2[sort[Integer.parseInt(zby[i])]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(zby[i])]][k], para.getQx00(), 1, yjxs*para.getQx02(), 1);
					}
			}
			/**
			 * 临时管理工作安排班组
			 */
			conditions = "TO_CHAR(startdate,'yyyy-MM-dd') = '" 
				+ ym+"-"+varday+"' and plantype=2";
			
			tempList = myService.getListByDHql("startdate as czkssj, enddate as czjssj,fzr as zbz,gs as gs", conditions, null, "", "TPlanWeekDetail s");

			for (HashMap<String, Object> map : tempList) {
				
				// 工时
				int gs = (Integer) (map.get("gs") == null ? 0 :  map.get("gs"));
				// 操作日期判断是否算夜间
				String czkssj = DateFormatTool.formate((Date) map.get("czkssj"), "HHmm");
				String czjssj = DateFormatTool.formate((Date) map.get("czjssj"), "HHmm");
				
				// 总负责人
				String[] zbz = ((String) (map.get("zbz") == null? "" : map.get("zbz"))).split(",");
				
				// 如果时间为空 就当 早上 9：30处理 按白天算 。一般不会出现 因为有操作人就有操作时间
				czkssj = "".equals(czkssj) ? "930" : czkssj;
				czjssj = "".equals(czjssj) ? "930" : czjssj;
				// 周末是否
				Date rq = ((Date) (map.get("czkssj") == null? "" : map.get("czkssj")));  
		        int n =DateFormatTool.dayOfWeek(rq, false);
				double zmxs=1;
				if (n==6||n==0) {
					zmxs=para.getXk21();
				}
				// 夜间倍数 乘 人/工作 除 实际多少人干了这个工作 
				double yjxs=1;
				if (Integer.parseInt(czkssj) >= yjks || Integer.parseInt(czkssj) <= yjjs || Integer.parseInt(czjssj) >= yjks || Integer.parseInt(czjssj) <= yjjs) {
				yjxs=para.getQx01();
				}
				
				// 工作负责人
				for (i = 0; i < zbz.length; i++) {
					
					if ("".equals(zbz[i])) {
						break;
					}
					
					// 20110329
					if (sort[Integer.parseInt(zbz[i])] == -1) {
						continue;
					}
					
					str2[sort[Integer.parseInt(zbz[i])]][k] = NumUtils.gzltjCzcs(str2[sort[Integer.parseInt(zbz[i])]][k],gs, 1, zmxs, 1, 1);
					
					}
				
				
			}
			/**
			 * 班组作业控制卡
			 */

			StringBuffer sql = new StringBuffer();
			 conditions = "TO_CHAR(DT_CZRQ,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"'";
		
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			//班组作业控制卡(一种票)
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, BXR AS CJR FROM TAB_KZK WHERE " + conditions + " AND DT_CZRQ IS NOT NULL AND OC_TYPE='01' GROUP BY CJRID,BXR ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				 double fzrxs=1;
				  fzrxs=para.getCz61();
				// 不管几个人都按照一次 
				//str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], para.getCz00(), 1, NumUtils.parseInt(map.get("count").toString())*para.getCz02(), 1);
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz32(), 1);
				//风险控制卡和一种票计分一样
				//str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], para.getCz00(), 1, NumUtils.parseInt(map.get("count").toString())*para.getCz02(), 1);
				
			}
			/**
			 * 工作计划(一种票)有停电范围的是一种票
			 */
			 sql = new StringBuffer();
			 conditions = "TO_CHAR(STARTDATE,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"'";
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			sql.append(" SELECT COUNT(*) AS C, fzr AS CJRID, RESPONSIBLE AS CJR FROM TAB_PLAN WHERE " + conditions + "  AND POWERRANGE is not null GROUP BY fzr,RESPONSIBLE ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz02(), 1);
				//风险控制卡和一种票计分一样
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz02(), 1);
				
			}
			/**
			 * 工作计划(二种票)无停电范围的是二种票
			 */
			 sql = new StringBuffer();
			
			 conditions = "TO_CHAR(STARTDATE,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"'";
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			sql.append(" SELECT COUNT(*) AS C, fzr AS CJRID, RESPONSIBLE AS CJR FROM TAB_PLAN WHERE " + conditions + "  AND POWERRANGE is null GROUP BY fzr,RESPONSIBLE ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz12(), 1);
				
			}
			//班组作业控制卡(二种票)
		    sql = new StringBuffer();
			 conditions = "TO_CHAR(DT_CZRQ,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"'";
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			//班组作业控制卡(二种票)
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, BXR AS CJR FROM TAB_KZK WHERE " + conditions + " AND DT_CZRQ IS NOT NULL AND OC_TYPE='02' GROUP BY CJRID,BXR ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				//str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], para.getCz00(), 1, NumUtils.parseInt(map.get("count").toString())*para.getCz12(), 1);
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz32(), 1);
				
			}
			/**
			 * 工作任务单录入人(录入时间在工作日期前一天15:00以前统计0.1分)
			 */
			 sql = new StringBuffer();
			 conditions = "TO_CHAR(rq,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"' AND TO_CHAR(CJSJ,'yyyy-MM-dd HH24:mi')<concat(TO_CHAR(rq-1,'yyyy-MM-dd'),' 15:00')";
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, CJR AS CJR FROM TAB_GZRWD WHERE " + conditions + " GROUP BY CJRID,CJR ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * 0.1, 1);
				
			}
			/**
			 * 工序质量控制卡
			 */
			 sql = new StringBuffer();
			 conditions = "TO_CHAR(DT_JHGZSJQ,'yyyy-MM-dd') = '" 
					+ ym+"-"+varday+"'";
		
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			//工序质量控制卡
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, OC_BXR AS CJR FROM TAB_GXZLKZK WHERE " + conditions + " AND DT_JHGZSJQ IS NOT NULL GROUP BY CJRID, OC_BXR ");	
	     	sql.append(" ) GROUP BY CJRID, CJR ");
			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz42(), 1);
				}

			/**
			 * 录入 录入时间
			 */
			 sql = new StringBuffer();
			 conditions = "TO_CHAR(cjsj,'yyyy-MM-dd') = '" 
					+ ym+"-"+varday+"'";
			 conditions1 = "TO_CHAR(JLRQ,'yyyy-MM-dd') = '" 
				 + ym+"-"+varday+"'";
			 conditions1= "TO_CHAR(rq,'yyyy-MM-dd') = '" 
				+ ym+"-"+varday+"'";
			sql.append("SELECT SUM(C) COUNT, CJRID AS CJRID, CJR AS CJR FROM ( ");
			//工作记录
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, JLR AS CJR FROM TAB_GZJL WHERE GZRWDID IN (SELECT ID FROM TAB_GZRWD WHERE " + conditions1 + ") AND JLRQ IS NOT NULL GROUP BY CJRID, JLR ");	
//			by wangtao add2020427
			// 设备缺陷记录
			sql.append(" UNION ALL");
			sql.append(" SELECT COUNT(*) AS C, CJRID AS CJRID, CJR AS CJR FROM TAB_SBQXJL WHERE " + conditions + " GROUP BY CJRID, CJR ");	
//			 设备缺陷记录录入人1
			sql.append(" UNION ALL");
			sql.append(" SELECT COUNT(*) AS C, CJRID1 AS CJRID, CJR1 AS CJR FROM TAB_SBQXJL WHERE " + conditions + " AND CJRID1 IS NOT NULL GROUP BY CJRID1, CJR1 ");	
			sql.append(" ) GROUP BY CJRID, CJR ");

			tempList = myService.findListBySql(sql.toString());
			
			// 录入直接查询出人的所有次数 直接 乘以 分值  人次 和夜间都不算了
			for (HashMap<String, Object> map : tempList) {
				if (sort[Integer.parseInt(map.get("cjrid").toString())] == -1) {
					continue;
				}	
				//str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] += String.valueOf((NumUtils.parseInt(map.get("count").toString()))*para.getCz52());
				str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k] = NumUtils.gzltj(str2[sort[Integer.parseInt(map.get("cjrid").toString())]][k], 1, 1 , NumUtils.parseInt(map.get("count").toString()) * para.getCz52(), 1);
				
			}
		}
		// 参与考核
		List<String[]> exp1 = new ArrayList();
		// 不参与考核
		List<String[]> exp2 = new ArrayList();
		
		for (i=0; i<str.length;i++) {
				// 未参与工作量考核人员 就是不参与的和未值班>21天的
			if ("2".equals(str[i][21])) {
				exp2.add(str[i]);
			} else {
				exp1.add(str[i]);
				exp3.add(str2[i]);
			}
		}
		
	
		return "GZLTJ_SUCCESS";
	}
	// 日均工作量统计得分
	public void getRjgzldf() throws IOException {
		StringBuffer sql = new StringBuffer();
		 conditions = "TO_CHAR(RQ,'yyyy-MM-dd') = '" 
			 + ym+'-'+day+"' and (ZBZNAME like '"+username+"' or ZBYNAME like '%"+username+"%')";
		sql.append(" SELECT * FROM (");
		// 工作任务单(有票）
		sql.append("SELECT ID AS ID,TO_CHAR(RQ,'YYYY-MM-DD') AS RQ,ZBZNAME||'('||GS||')' AS FZRNAME,ZBYNAME AS CY,");
		sql.append("	(select REPLACE(ltrim(sys_connect_by_path(REPLACE(gznr,id, ''), '|'), '|'),'|','★') as gznr");
		sql.append("		from (select GZRWDID, id, (id ||sbname|| gznr) as gznr , count(*) over(partition by GZRWDID) cnt from TAB_GZRWD_RW d order by id) ss");
		sql.append("		where level = cnt");
		sql.append("		and ss.GZRWDID = JL.id");
		sql.append("		connect by prior GZRWDID = GZRWDID");
		sql.append("		and prior (id || gznr) < (id || gznr)) as GZRW,");
		sql.append(" REMARK AS BZ");
		sql.append(" FROM TAB_GZRWD JL");
		sql.append(" WHERE  (JL.STATE='0' OR JL.STATE='9') AND "); //  
		sql.append(conditions);
		// 工作任务单(无票）
		sql.append(" UNION ");
		sql.append("SELECT ID AS ID,TO_CHAR(RQ,'YYYY-MM-DD') AS RQ,ZBZNAME||'('||GS||')' AS FZRNAME,ZBYNAME AS CY,YCCLQK AS GZRW,REMARK AS BZ");
		sql.append(" FROM TAB_GZRWD JL");
		sql.append(" WHERE  (JL.STATE='5' OR JL.STATE='6') AND "); //  
		sql.append(conditions);
		// 设备缺陷消缺
		 conditions = "TO_CHAR(xcjssj,'yyyy-MM-dd') = '" 
			 + ym+'-'+day+"' and (xcrname like '"+username+"' or xqcyname like '%"+username+"%')";
		sql.append(" UNION ");
		sql.append("SELECT ID AS ID,TO_CHAR(xcjssj,'YYYY-MM-DD') AS RQ,xcrname AS FZRNAME,xqcyname AS CY,qxnr||sjxqqk AS GZRW,ylwt AS BZ");
		sql.append(" FROM TAB_SBQXJL JL");
		sql.append(" WHERE   "); //  
		sql.append(conditions);
		sql.append(") ORDER BY ID");
		List<HashMap<String, Object>> dicList = myService
				.findListBySql(sql.toString());
		JsonConfig jsonConfig = JsonUtils.configJson(new String[] {},
				"yyyy-MM-dd");
		JSON json = JSONSerializer.toJSON(dicList, jsonConfig);
		 String rtn = "{result:" + json.toString()+"}";
		super.getResponse().getWriter().print(rtn);
	}
	public String getRjgzltj(){
		return "GZLTJFRAME_SUCCESS";
	}
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setMyService(JcCommonService myService) {
		this.myService = myService;
	}

	public List<TPlanMonth> getPlanMonthList() {
		return planMonthList;
	}

	public void setPlanMonthList(List<TPlanMonth> planMonthList) {
		this.planMonthList = planMonthList;
	}

	public String getYm() {
		return ym;
	}

	public void setYm(String ym) {
		this.ym = ym;
	}

	public String getBz() {
		return bz;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(String isAdmin) {
		this.isAdmin = isAdmin;
	}

	public List<String[]> getExp3() {
		return exp3;
	}

	public void setExp3(List<String[]> exp3) {
		this.exp3 = exp3;
	}

	public String getDay() {
		return day;
	}

	public void setDay(String day) {
		this.day = day;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	
}
